package com.git.service.impl;

import com.git.bean.District;
import com.git.bean.House;
import com.git.bean.SearchInfo;
import com.git.mapper.DistrictMapper;
import com.git.mapper.HouseMapper;
import com.git.service.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 金振宇
 * @describe
 */
@Service
@Component
public class HouseServiceImpl implements HouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private DistrictMapper districtMapper;

    @Override
    public PageInfo<House> findAllHouse(Integer pageNum, SearchInfo searchInfo) {
        PageHelper.startPage(pageNum,3);
        //处理搜索内容
        if(searchInfo.getPriceStr()==null || "".equals(searchInfo.getPriceStr())){
            searchInfo.setStartPrice(Integer.valueOf(0));
            searchInfo.setEndPrice(Integer.valueOf(0));
        }else {
            String[] split = searchInfo.getPriceStr().split("-");
            System.out.println("价钱切割之后");
            for (String s : split) {
                System.out.println(s);
            }
            searchInfo.setStartPrice(Integer.valueOf(split[0]));
            searchInfo.setEndPrice(Integer.valueOf(split[1]));
        }
        List<House> list = houseMapper.selectAllHouse(searchInfo);
        PageInfo<House> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public House findHouseById(Integer id) {
        return houseMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<District> findAllDistrict() {
        return districtMapper.selectAllDistrict();
    }



    @Override
    public void updateHouse(House house) {
        houseMapper.updateByPrimaryKeySelective(house);
    }

    @Override
    public void addHouse(House house) {
        houseMapper.insertSelective(house);
    }
}
